package org.hu.mailserver.receiver;

import org.hu.vhr.model.Employee;
import org.hu.vhr.model.Interview;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MailSenderClass {
    public static final Logger logger = LoggerFactory.getLogger(MailSenderClass.class);
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    MailProperties mailProperties;
    @Autowired
    TemplateEngine templateEngine;




    @RabbitListener(queues = "hcy.mail.welcome")
    public void handler(Employee employee){
        System.out.println(employee.toString());
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(msg);

        try {
            mimeMessageHelper.setTo(employee.getEmail());
            mimeMessageHelper.setFrom("htrees@qq.com");
            mimeMessageHelper.setSubject("入职欢迎");
            mimeMessageHelper.setSentDate(new Date());

            Context context = new Context();
            context.setVariable("name",employee.getName());
            context.setVariable("posname",employee.getPosition().getName());
            context.setVariable("joblevelname",employee.getJoblevel().getName());
            context.setVariable("departementname",employee.getDepartment().getName());
            String mail = templateEngine.process("mail", context);
            mimeMessageHelper.setText(mail,true);
            javaMailSender.send(msg);

        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("邮件发送失败");
        }
    }


    @RabbitListener(queues = "hcy.mail.interview")
    public void handler1(Interview interview){
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(msg);

        try {
            mimeMessageHelper.setTo(interview.getToeamil1());
            mimeMessageHelper.setFrom("htrees@qq.com");
            mimeMessageHelper.setSubject("面试邀请");
            mimeMessageHelper.setSentDate(new Date());

            Context context = new Context();
            context.setVariable("name", interview.getName());
            context.setVariable("time",new SimpleDateFormat("yyyy-MM-dd HH:mm").format(interview.getTime()));
            String mail = templateEngine.process("mail1", context);
            mimeMessageHelper.setText(mail,true);
            javaMailSender.send(msg);

        } catch (MessagingException e) {
            e.printStackTrace();
            logger.error("邮件发送失败");
        }
    }
}
